<?php

/* 
 * 内容回收
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class storyContentBakController extends AppController
{
    public $class = 'storycontentbak';
    public function __construct() {
        parent::__construct();
        $this->modelObj = new storyContentBakModel();
        $this->log_type = '内容回收';
        $this->cookie_key = 'story_contentbak_cookie';
        $this->assign('class', $this->class);
    }
    public function view(){
        $this->title = '内容';
        $where = array('is_show' => 1);
        $data = storyTitleModel::load()->getAll(null, $where); 
        $this->assign('title', $data);
        $this->_get_data();
        $this->display('contentbak_list');
    }
    public function query(){
        $this->_get_data();
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('contentbak_list');
        exit(json_encode($this->ret));
    }
    public function restore(){
        $id = intval(trim($_POST['id']));
        if($id > 0) {
            $bool = storyContentBakModel::load()->restore($id);
            if($bool) {
                exit($this->setReturn(0, '恢复成功'));
            }
        }
        exit($this->setReturn(100, '恢复失败'));
    }

    /**
     * 来源获取数据
     */
    private function _get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;  
        $filter['tid'] = isset($_POST['tid']) ? $_POST['tid'] : 0;
        $filter['replyid'] = isset($_POST['replyid']) ? $_POST['replyid'] : 0;
        $filter['content'] = isset($_POST['tid']) ? $_POST['tid'] : '';
        if($filter['tid'] > 0) {
            $where .= ' and tid = '. $filter['tid'];
        }
        if($filter['replyid'] > 0) {
            $where .= ' and replyid = '. $filter['replyid'];
        }
        if(!empty($filter['content'])) {
            $where .= " and instr(content, '{$filter['content']}')";
        }
        $key = $this->modelObj->keyFields;
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $key;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('story_content_bak', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);
        $list = $data['pageData'];
        if($list) {
            foreach ($list as $key => $row) {           
                $data['pageData'][$key]['id'] = $key + 1;
                $tname = storyTitleModel::load()->getName($row['tid']);
                $content = $row['content'];
                $data['pageData'][$key]['tname'] = p_str_len($tname) > 20 ? p_cut_str($tname, 20) . '...' : $tname;
                $data['pageData'][$key]['content'] = p_str_len($content) > 20 ? p_cut_str($content, 20) . '...' : $content; 
                $data['pageData'][$key]['in_time_format'] = publicController::load()->friendlyDate($row['in_time']);               
                $data['pageData'][$key]['is_show'] = $row['is_show'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';  
            }
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }
}

